package C50;

import java.util.Arrays;

/**
 * ClassName: C04
 * Package: C50
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/28 11:17
 * @Version 1.0
 * @Since 1.0
 */
public class C04 {
    public static void main(String[] args) {
//        int[] arr = {3, 2, 2, 1};
        int[] arr = {1, 2};
        System.out.println(numRescueBoats(arr, 3));
    }


    public static int numRescueBoats(int[] arr, int limit) {
        Arrays.sort(arr);
        int l = 0, r = arr.length - 1, ans = 0;
        while (l < r) {
            if (arr[l] + arr[r] <= limit) {
                l++;
                r--;
                ans++;
            } else {
                ans++;
                r--;
            }
        }
        if(l == r && arr[l] <= limit) {
            ans ++;
        }
        return ans;
    }
}
